package com.xm2.demo.system.employee.service;

import com.xm2.demo.model.User;
import com.xm2.demo.system.employee.dao.EmployeeRepository;
import com.xm2.demo.system.employee.dto.EmployeeDto;
import com.xm2.demo.system.employee.dto.EmployeePage;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Md5;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Create
 * @create 2018-12-17 18:55
 * @Description：
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    public Pagination findByPageable(Pagination pagination, Integer pId) {
        pagination.setTotalRows(employeeRepository.findAllByPid(pId));
        pagination.setList(employeeRepository.findUserByPidPage(pId, pagination.getStartRow(), pagination.getSize()));
        return pagination;
    }

    @Override
    public List<String> getPosition(Integer pid) {
        List<String> lists = new ArrayList<>();
        if (pid == 2) {
            lists = employeeRepository.findStoreByPId();
        } else if (pid == 3) {
            lists = employeeRepository.findCenterByPId();
        }
        return lists;
    }

    @Override
    public int save(EmployeeDto employeeDto, Integer pid) {
        User user = new User();
        user.setPassword(Md5.getMD5("12345"));
        user.setPositionId(pid);
        //  查询手机号是否已存在
        User userInfo = employeeRepository.findUserByPhone(employeeDto.getPhone());
        if (employeeDto.getId() == null) {
            // 添加
            if (userInfo == null){
                user.setName(employeeDto.getName());
                user.setPhone(employeeDto.getPhone());
                user.setUnit(employeeDto.getUnit());
                employeeRepository.save(user);
                return 1;
            }
            return 0;
        } else if (employeeDto.getId() != null){
            //更新
            User update = employeeRepository.findOne(employeeDto.getId());
            // 是否更新手机号
            if (update.getPhone().equals(employeeDto.getPhone())){
                BeanUtils.copyProperties(employeeDto, user);
                employeeRepository.save(user);
                return 1;
            } else if (!update.getPhone().equals(employeeDto.getPhone())){
                if (userInfo == null){
                    BeanUtils.copyProperties(employeeDto, user);
                    employeeRepository.save(user);
                    return 1;
                }
            }
        }
        return 0;
    }

    @Override
    public void deleteByIds(String ids) {
        // 接收包含stuId的字符串，并将它分割成字符串数组
        String[] stuList = ids.split(",");
        // 将字符串数组转为List<Intger> 类型
        List<Integer> LString = new ArrayList<Integer>();
        for (String str : stuList) {
            LString.add(new Integer(str));
        }
        employeeRepository.deleteByIds(LString);
    }

    @Override
    public Pagination findByEmployeePage(EmployeePage employeePage, Integer pid) {
        List<User> lists = new ArrayList<>();
        User user = employeeRepository.findUserByPhone(employeePage.getPhone(), pid);
        lists.add(user);
        employeePage.setTotalRows(employeeRepository.findAllByPid(pid));
        employeePage.setList(lists);
        return employeePage;
    }


}
